package com.luxsoft.sw3.kio.ui.consultas;

import java.util.List;

import javax.swing.Action;
import javax.swing.JPopupMenu;

import com.luxsoft.siipap.pos.ui.forms.FacturaForm;
import com.luxsoft.siipap.pos.ui.utils.FacturaUtils;
import com.luxsoft.siipap.pos.ui.utils.ReportUtils2;
import com.luxsoft.siipap.swing.browser.FilteredBrowserPanel;
import com.luxsoft.siipap.swing.utils.CommandUtils;

import com.luxsoft.sw3.gasoc.model.Factura;
import com.luxsoft.sw3.services.Services;




public class FacturasKyoCafePanel extends FilteredBrowserPanel<Factura>{
	
	 

	public FacturasKyoCafePanel() {
		super(Factura.class);
		setTitle("Facturas generadas");		
	}
	
	public void init(){
		addProperty(
				"serie","tipo","folio","fecha","nombre","total"
				);
		addLabels(
				"Serie","Tipo","Folio","Fecha","Nombre","Total"
				);
		installTextComponentMatcherEditor("Serie", "serie");
		installTextComponentMatcherEditor("Folio", "folio");
		installTextComponentMatcherEditor("Tipo", "tipo");
		installTextComponentMatcherEditor("Total", "total");
		installTextComponentMatcherEditor("Cliente", "nombre");
		manejarPeriodo();
	}
	
	@Override
	protected void afterGridCreated() {		
		super.afterGridCreated();
		JPopupMenu popup=new JPopupMenu("Operaciones");
		for(Action a:getActions()){
			popup.add(a);
		}
		getGrid().setComponentPopupMenu(popup);
	}
	
	@Override
	protected List<Factura> findData() {
		String hql="from Factura c where c.fecha " +
				"between ? and ? and c.tipo in(\'CAFE\',\'VENTA_GENERAL\')";
		return Services.getInstance().getHibernateTemplate().find(hql
				, new Object[]{periodo.getFechaInicial()
				,periodo.getFechaFinal()}
				);
	}
	
	
	@Override
	public Action[] getActions() {
		if(actions==null)
			actions=new Action[]{
				getLoadAction()
				,getInsertAction()
				//,getDeleteAction()
				//,getEditAction()
				,getViewAction()
				,addAction(null, "generarCFDI", "Generar CFDI")
				//,CommandUtils.createPrintAction(this, "imprimir")
				};
		return actions;
	}
	
	@Override
	protected List<Action> createProccessActions(){
		List<Action> res=super.createProccessActions();
		//res.add(addAction(null, "mandarPorCorreoElectronico", "Mandar por Correo"));
		return res;
	}
	
	public void imprimir(){
		Factura fac=(Factura)getSelectedObject();
		if(fac!=null){
			FacturaUtils.imprimirFactura(fac, true);
		}
	}

	@Override
	protected void doSelect(Object bean) {
		Factura fac=(Factura)getSelectedObject();
		if(fac!=null){
			FacturaUtils.imprimirFactura(fac, true);
		}
		
	}
	
//	public void generarCFDI(){
//		Factura fac=(Factura)getSelectedObject();
//		if(fac!=null){
//			FacturacionController.generarCFDI(fac);
//			
//		}
//	}

}
